package tt.com.warehousemanager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import tt.com.common.enums.Status;
import tt.com.common.model.YxCommonWarehouseGoods;
import tt.com.common.util.CommonResult;
import tt.com.common.util.ObjectUtil;
import tt.com.common.util.PageUtil;
import tt.com.warehousemanager.service.ProvidersService;
import tt.com.warehousemanager.service.WarehouseGoodsService;

import javax.servlet.http.HttpSession;

/**
 * @author Czycka
 * @date 2022/8/19 0:16
 * @description 仓库物品服务控制层
 */
@RestController
@RequestMapping(value = "warehouse/goods")
public class WarehouseGoodsController {

    @Autowired
    private WarehouseGoodsService goodsService;

    @Autowired
    private ProvidersService providersService;


    @PostMapping(value = "insert")
    public CommonResult insert(@RequestBody YxCommonWarehouseGoods goods, HttpSession session) {
        if (!ObjectUtil.objectIsNull(goods)) {
            return CommonResult.buildR(Status.PARAM_ISNULL,"参数为空");
        }
        Integer result = goodsService.insertGoods(goods, "2", providersService.getById(goods.getTargetId()));
        if (result == 0) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "物品信息插入失败");
        } else if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "物品信息已存在");
        } else {
            return CommonResult.buildR(Status.OK, "物品信息插入成功");
        }
    }

    @PostMapping(value = "delete/{id}")
    public CommonResult delete(@PathVariable("id") String id) {
        if (id == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL);
        }
        Integer result = goodsService.deleteGoods(Long.parseLong(id), "22");
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "物品不存在");
        } else if (result == -2) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "无法重复删除");
        } else {
            return CommonResult.buildR(Status.OK, "删除成功");
        }
    }

    @PostMapping(value = "update")
    public CommonResult update(@RequestBody YxCommonWarehouseGoods goods) {
        if (!ObjectUtil.objectIsNull(goods)) {
            return CommonResult.buildR(Status.PARAM_ISNULL,"参数为空");
        }
        if (goodsService.updateGoods(goods, "2") > 0) {
            return CommonResult.buildR(Status.OK, "物品修改成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "物品修改失败");
        }
    }

    @PostMapping(value = "show/page")
    public CommonResult showWarehouseGoods(@RequestBody PageUtil pageUtil) {
        if (pageUtil.getPageIndex() == null || pageUtil.getPageSize() == null) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "参数为空");
        }
        pageUtil = goodsService.showGoodsByPage(pageUtil);
        return CommonResult.buildR(Status.OK,"查询成功", pageUtil);
    }

    @PostMapping(value = "show/all")
    public CommonResult showAll(){
        return CommonResult.buildR(Status.OK,"查询成功",goodsService.list());
    }

    @PostMapping(value = "show/{id}")
    public CommonResult showById(@PathVariable("id") String id){
        if (StringUtils.isEmpty(id)) {
            return CommonResult.buildR(Status.SYSTEM_ERROR,"参数为空");
        }
        return CommonResult.buildR(Status.OK,"查询成功",goodsService.getById(id));
    }

}
